package com.kpx.app;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.os.StrictMode;
import android.support.multidex.MultiDexApplication;

import com.kpx.app.listener.GlideImageLoader;
import com.kpx.app.listener.GlidePauseOnScrollListener;
import com.kpx.app.service.UploadService;
import com.kpx.app.thirdparty.bugly.CrashReporterUtils;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechUtility;
import com.squareup.leakcanary.RefWatcher;
import com.kpx.app.configs.Constants;
import com.kpx.app.utils.UniversualImageLoaderUtils;

import cn.finalteam.galleryfinal.CoreConfig;
import cn.finalteam.galleryfinal.FunctionConfig;
import cn.finalteam.galleryfinal.GalleryFinal;
import cn.finalteam.galleryfinal.ThemeConfig;
import cn.jpush.android.api.JPushInterface;

/**
 * Created by Ryan
 * 类描述 ： 应用实例
 * 创建时间 ：  2015/1/23 13:12.
 */
public class Application extends MultiDexApplication {
    public static Application instance;
    public static boolean isHttps = false;
    private RefWatcher refWatcher;
    public static Context mContext;
    public static long releaseTime;//用于矫正手机时间
    public static final int REQUEST_CODE_GALLERY = 1001;

    private final class InitHandler extends Handler {
        public InitHandler(Looper looper) {
            super(looper);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (Constants.DEBUG) {
//                refWatcher = LeakCanary.install(Application.this);
            }
        }
    }

    public Application() {
        instance = this;
    }

    public static RefWatcher getRefWatcher(Context context) {
        Application application = (Application) context.getApplicationContext();
        return application.refWatcher;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = getApplicationContext();
        startService(new Intent(mContext, UploadService.class));
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
            StrictMode.setVmPolicy(builder.build());
        }
        instance = this;
        initPhotoSelect();
        UniversualImageLoaderUtils.initImageLoader(getApplicationContext());
        initBugly();
//        try {
//            initSpeech();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        HandlerThread initThread = new HandlerThread("Init Thread");
        initThread.start();
        InitHandler mInitHandler = new InitHandler(initThread.getLooper());
        mInitHandler.sendEmptyMessage(0);
    }

    private void initPhotoSelect() {
        //设置主题
        ThemeConfig theme = ThemeConfig.DARK;
//        ThemeConfig theme = new ThemeConfig.Builder().build();
        //配置功能
        FunctionConfig functionConfig = new FunctionConfig.Builder()
                .setEnableCamera(true)
                .setEnableEdit(true)
                .setEnableCrop(true)
                .setEnableRotate(true)
                .setCropSquare(true)
                .setEnablePreview(true)
                .build();
        CoreConfig coreConfig = new CoreConfig.Builder(this, new GlideImageLoader(), theme)
                .setFunctionConfig(functionConfig)
                .setPauseOnScrollListener(new GlidePauseOnScrollListener(false, true))
                .build();
        GalleryFinal.init(coreConfig);

    }

    /**
     * 初始化腾讯Bugly异常上报库
     */
    private void initBugly() {
        CrashReporterUtils.initCrashReport(this);
    }


    /**
     * 初始化JPush
     */
    private void initJPush() {
        JPushInterface.requestPermission(this);
        JPushInterface.setDebugMode(Constants.DEBUG);// 设置开启日志,发布时请关闭日志
        JPushInterface.init(this); // 初始化 JPush
    }

    /**
     * 初始化科大讯飞语音合成
     */
    private void initSpeech() {
        StringBuffer param = new StringBuffer();
        param.append("appid=56d02bd3");
        param.append(",");
        param.append(SpeechConstant.ENGINE_MODE + "=" + SpeechConstant.MODE_MSC);
        param.append(",");
        //此接口在非主进程调用会返回null对象，如需在非主进程使用语音功能，请增加参数：SpeechConstant.FORCE_LOGIN+"=true"
        param.append(SpeechConstant.FORCE_LOGIN + "=true");
        SpeechUtility.createUtility(this, param.toString());
    }

}
